﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LcfContols
{
    /// <summary>
    /// 自定义Tab控件
    /// </summary>
    public class LTabControl:TabControl
    {
        #region 私有属性
        /// <summary>
        /// 标签栏文字颜色
        /// </summary>
        private Color TabFontColor = SystemColors.WindowText;

        /// <summary>
        /// 选中标签下划线颜色
        /// </summary>
        private Color _selectLineColor = Color.LightSeaGreen;

        /// <summary>
        /// 线条颜色
        /// </summary>
        private Color _LineColor = DefaultBackColor;

        /// <summary>
        /// 选项卡线高度
        /// </summary>
        private int _LineHeight = 3;
        #endregion

        /// <summary>
        /// 选中时候的背景图片
        /// </summary>
        public Image SelectTabBackImage { get; set; }

        /// <summary>
        /// 标签栏大小
        /// </summary>
        public Size TabSize
        {
            get
            {
                return ItemSize;
            }
            set
            {
                ItemSize = value;
                Invalidate(new Rectangle(ClientRectangle.X, ClientRectangle.Y, DisplayRectangle.Width, DisplayRectangle.Y - ClientRectangle.Y)) ;
            }
        }

        /// <summary>
        /// 设置标签栏字体颜色
        /// </summary>
        public Color TabTextColor
        {
            get
            {
                return TabFontColor;
            }
            set
            {
                TabFontColor = value;
                Invalidate(new Rectangle(ClientRectangle.X,ClientRectangle.Y,DisplayRectangle.Width,DisplayRectangle.Y-ClientRectangle.Y));
            }
        }

        /// <summary>
        /// 选中标签下端线颜色
        /// </summary>
        public Color SelectLineColor
        {
            get
            {
                return _selectLineColor;
            }
            set
            {
                _selectLineColor = value;
                if (SelectedIndex > 0)
                {
                    Rectangle bounds = GetTabRect(SelectedIndex);
                    Invalidate(new Rectangle(Math.Max(bounds.X, DisplayRectangle.X), bounds.Y + bounds.Height, bounds.Width, _LineHeight));
                }
            }
        }

        /// <summary>
        /// 选项卡线条颜色
        /// </summary>
        public Color LineColor
        {
            get
            {
                return _LineColor;
            }
            set
            {
                _LineColor = value;
                Invalidate(new Rectangle(DisplayRectangle.X, DisplayRectangle.Y - _LineHeight, DisplayRectangle.Width, _LineHeight));
            }
        }

        public LTabControl()
        {
            SetStyle(
                ControlStyles.UserPaint | // 控件将自行绘制，而不是通过操作系统来绘制  
                ControlStyles.OptimizedDoubleBuffer | // 该控件首先在缓冲区中绘制，而不是直接绘制到屏幕上，这样可以减少闪烁 
                ControlStyles.AllPaintingInWmPaint | // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁  
                ControlStyles.ResizeRedraw | // 在调整控件大小时重绘控件 
                ControlStyles.SupportsTransparentBackColor,// 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明
                true // 设置以上值为 true 
                );
            UpdateStyles();
            SizeMode = TabSizeMode.Fixed;
        }
        
        protected new Rectangle GetTabRect(int index)
        {
            Rectangle result=base.GetTabRect(index);
            result.Height -= _LineHeight - (DisplayRectangle.Y-result.Height-result.Y);
            return result;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            using (SolidBrush solidbrush = new SolidBrush(_LineColor))
            {
                e.Graphics.FillRectangle(solidbrush, new Rectangle(DisplayRectangle.X, DisplayRectangle.Y- _LineHeight, DisplayRectangle.Width, _LineHeight));
            }
            for (int i = 0; i < TabCount; i++)
            {
                Rectangle bounds = GetTabRect(i);
                Rectangle downLine = new Rectangle(Math.Max(bounds.X,DisplayRectangle.X), bounds.Y + bounds.Height , bounds.Width, _LineHeight);
                if (SelectedIndex == i) //绘制选中窗口背景
                {
                    using (SolidBrush soldbrush = new SolidBrush(_selectLineColor))
                    {
                        e.Graphics.FillRectangle(soldbrush, downLine);
                    }
                    if (SelectTabBackImage != null) //如果没有设置背景图片则使用渐变
                        e.Graphics.DrawImage(SelectTabBackImage, bounds);
                }
                SizeF textSize = TextRenderer.MeasureText(TabPages[i].Text, this.Font); //获取文字大小
                PointF textPoint = new PointF();
                // 注意要加上每个标签的左偏移量X  
                textPoint.X = bounds.X + (bounds.Width - textSize.Width) / 2;
                textPoint.Y = bounds.Bottom - textSize.Height - Padding.Y;
                // Draw highlights  
                e.Graphics.DrawString(
                    TabPages[i].Text,
                    Font, SystemBrushes.ControlLightLight,    // 高光颜色  
                    textPoint.X,
                    textPoint.Y);
                // 绘制正常文字  
                textPoint.Y--;
                using(SolidBrush brush=new SolidBrush(TabTextColor))
                {
                    e.Graphics.DrawString(
                    TabPages[i].Text,
                    Font,
                    brush, // 正常颜色  
                    textPoint.X,
                    textPoint.Y);
                }
                // 绘制图标  
                if (ImageList != null && i < ImageList.Images.Count)
                {
                    int index = TabPages[i].ImageIndex;
                    string key = TabPages[i].ImageKey;
                    Image icon = new Bitmap(1, 1);
                    if (index > -1)
                    {
                        icon = ImageList.Images[index];
                    }
                    else if (!string.IsNullOrEmpty(key))
                    {
                        icon = ImageList.Images[key];
                    }
                    e.Graphics.DrawImage(
                        icon,
                        bounds.X + (bounds.Width - icon.Width) / 2,
                        bounds.Top + Padding.Y);
                }
            }
        }

    }
}
